import PAGE from '../../../utils/middleware/page'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import store from '../../../store/index'
const app = getApp()
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            needRefresh: 'vehicle.needRefresh'
        },
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh'
        }
    },
    data: {
        info: {},
        qrcodeNum: null,
        road: null,
        apiField: {
            module: 'charge',
            api: 'Scan',
            data: 'info',
            search: {
                qrcodeNum: null
            }
        }
    },
    async onLoad(options) {
        this.handleQrcodeParams(options)
        this.data.wxLoginRes = await wx.$Fun.wxLogin()
    },
    handleQrcodeParams(options) {
        if (options && options.q) {
            var scene = decodeURIComponent(options.q)
            var params = scene.split('?')[1]
            this.data.qrcodeNum = wx.$util.getUrlParams('qrcodenum', params);
            this.data.road = wx.$util.getUrlParams('road', params);
            //this.fetchData()
        } else {
            wx.$Fun.showToast("扫码参数错误")
            //this.data.qrcodeNum = '230056'
            //this.fetchData()
        }
    },
    onShow() {
        this.fetchData()
    },
    onPullDownRefresh() {
        this.fetchData();
        wx.stopPullDownRefresh();
    },
    async fetchData() {
        this.data.apiField.search.qrcodeNum = this.data.qrcodeNum
        this.handleFetchData({
            error: (e) => {
                wx.$Fun.showToast(e.Message)
            }
        }, 'apiField', true)
    },
    async login(options) {
        try {
            const res = await wx.$Api.member.Login({
                data: {
                    Code: this.data.wxLoginRes.code
                }
            })
            if (res.Data.Status == 0) {
                this.setData({
                    showGetPhone: true
                })
            }
            else {
                //已完善获取token
                this.getToken(res.Data.Token, () => {
                    this.toPay(options)
                })
            }
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
        finally {
            this.data.wxLoginRes = await wx.$Fun.wxLogin()
        }
    },
    // 注册前先解密手机
    async register(event) {
        if (event.detail.errMsg !== 'getPhoneNumber:ok') return
        try {
            var request = {
                WxCode: this.data.wxLoginRes.code,
                IV: event.detail.iv,
                EncryptedData: event.detail.encryptedData
            }
            const res = await wx.$Api.member.Register({ data: request })
            //已完善获取token
            this.getToken(res.Data.Token, () => {
                this.toPay()
            })

        } catch (e) {
            wx.$Fun.showToast(e.Message)
        }
        finally {
            this.data.wxLoginRes = await wx.$Fun.wxLogin()
        }
    },
    getToken(token, cb) {
        var _this = this
        wx.setStorage({
            key: 'token',
            data: `bearer ${token.AccessToken}`,
            success() {
                app.globalData.isLogin = true
                _this.updateNeedRefresh(true)
                _this.setData({
                    showGetPhone: false
                })
                typeof cb === 'function' && cb()
            }
        })
    },
    handlePhoneCall(e) {
        var phone = e.currentTarget.dataset.phone
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    handleToPay(e) {
        var item = e.currentTarget.dataset.item
        if (item.IsUsing != 0) {
            return
        }
        this.data.road = item.Road
        if (!app.globalData.isLogin) {
            this.login()
        } else {
            this.toPay()
        }
    },
    toPay() {
        wx.$Fun.toPage({
            page: 'chargePay',
            params: {
                '0': this.data.info.Id,
                '1': this.data.road
            }
        })
    }
}))